package com.otod.office;

import java.io.File;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.otod.base.exception.AppException;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.AttachmentConstant;
import com.otod.commons.constants.PropertiesConstant;
import com.otod.commons.tools.UserEnv;
import com.otod.commons.utils.RandomUtil;
import com.otod.commons.utils.SpringContextHolder;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.file.service.IFileService;
import com.otod.sys.vo.FileVo;
@Controller
@RequestMapping("office/wordPDF")
public class EditWordAndToPDFController extends BaseController{
	
	private static String PATH = UserEnv.getProp(PropertiesConstant.UPLOAD_FILE_PATH);
	
	
	
	/**
	 * 打开服务器文件
	 * @param request
	 * @param model
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value = "openWord", method = RequestMethod.GET)
	public String openWord(HttpServletRequest request, Model model) throws AppException {

		String empNo =  SessionUserUtil.getCurrentUser().getUserNo();
		String fileName = request.getParameter("fileName");
		String fileId = request.getParameter("fileId");
		String filePath = PATH+"document";
		if(StringUtils.isNotEmpty(fileId)){
			
			IFileService busiFileInfo = (IFileService)SpringContextHolder.getBean(IFileService.BEAN_ID); 
			FileVo fileVo = busiFileInfo.getById(fileId);
			if(fileVo ==null){
				throw new AppException("文件不存在，或已被删除");
			}
			fileName = fileVo.getFileServerName();
			filePath = PATH+"common";
		}
		String docPath = StringUtils.replace( filePath+File.separator+File.separator+fileName, "/", "\\\\");
		//PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
		/*poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
		poCtrl1.setSaveFilePage("SaveNewWord.do");//如要保存文件，此行必须
		poCtrl1.addCustomToolButton("保存", "Save()", 1);//添加自定义工具栏按钮
		poCtrl1.setCaption("妙策科技office控件");
		//隐藏菜单栏
		poCtrl1.setMenubar(false);
		//隐藏工具栏
		poCtrl1.setCustomToolbar(false);*/
		
		//--------------打开文件路径问题根据传入参数设置
		//String path  = PATH+"document"+File.separator;
		
		//poCtrl1.webOpen(docPath, OpenModeType.docNormalEdit, empNo);
		//poCtrl1.setTagId("PageOfficeCtrl1"); //此行必须	 
		
		return "office/editWord/editWord";
	}
	
	@RequestMapping(value = "combineWord", method = RequestMethod.GET)
	public String combineWord(HttpServletRequest request, Model model) {
		/*String path  = PATH+"document"+File.separator;
		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
		poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须

		String empNo =  SessionUserUtil.getCurrentUser().getUserNo();
		
		WordDocument worddoc = new WordDocument();
		//先在要插入word文件的位置手动插入书签,书签必须以“PO_”为前缀
		//给DataRegion赋值,值的形式为："[word]word文件路径[/word]、[excel]excel文件路径[/excel]、[image]图片路径[/image]"
		DataRegion data1 = worddoc.openDataRegion("PO_reg1");
		data1.setValue("[word]"+path+request.getParameter("document1")+"[/word]");
		//--------------需要设置路径
		DataRegion data2 = worddoc.openDataRegion("PO_reg2");
		data2.setValue("[word]"+path+request.getParameter("document2")+"[/word]");
		DataRegion data3 = worddoc.openDataRegion("PO_reg3");
		data3.setValue("[word]"+path+request.getParameter("document3")+"[/word]");

		poCtrl1.setWriter(worddoc);
		poCtrl1.setCaption("妙策科技office控件");
		poCtrl1.addCustomToolButton("保存", "Save()", 1);//添加自定义工具栏按钮
		poCtrl1.addCustomToolButton("另存为PDF文件", "SaveAsPDF()", 1);
		poCtrl1.setSaveFilePage("SaveNewWord.do");

		//隐藏菜单栏
		poCtrl1.setMenubar(false);
		//隐藏自定义工具栏
		//poCtrl1.setCustomToolbar(false);
		
		String fileName = request.getParameter("docTp");
		String pdfName = fileName.substring(0, fileName.length() - 4) + ".pdf";
		//--------------需要设置文件名称
		poCtrl1.webOpen(path+fileName, OpenModeType.docNormalEdit, empNo);
		poCtrl1.setTagId("PageOfficeCtrl1"); //此行必须
		*/
		return "office/editWord/combineWord";
		
	}
	
	@RequestMapping(value = "SaveNewWord")
	public String SaveNewWord(HttpServletRequest request, HttpServletResponse response,Model model) throws AppException {
		
		/*//定义保存对象import com.otod.commons
		//FileSaver fs = new FileSaver(request, response);
		//保存文件
		
		//String PATH = UserEnv.getProp(PropertiesConstant.UPLOAD_FILE_PATH);
		String path = PATH + "document" + File.separator;
		
		File pathFile = new File(path);
		boolean isPathExist = pathFile.exists();
		if (!isPathExist)
		     pathFile.mkdirs();*/
		/*fs.saveToFile(path +fs.getFileName());
		//设置保存结果
		IFileService busiFileInfo = (IFileService)SpringContextHolder.getBean(IFileService.BEAN_ID); 
		String id = RandomUtil.getUUID();
		FileVo bo = new FileVo();
			bo.setUuid(id);
			bo.setFileName(fs.getFileName());
			//bo.setFileType(funcType);											//功能类型
			bo.setFileFormat(fs.getFileExtName());
			bo.setFileSize(BigDecimal.valueOf(fs.getFileSize()));
			//bo.setDepict(StringUtils.isEmpty(smartRequest.getParameter("fileDesc"))?"无":smartRequest.getParameter("fileDesc"));
			bo.setFileServerPath(path);
			bo.setFileServerName(fs.getFileName());
			//新增时，附件的业务ID还没有生成，先传成默认KEY，需要保存成功时更新
			bo.setBusitableKey(AttachmentConstant.ATTACH_EMPTYBUSIKEY);
			//此处需要添加当前任务ID--参数传递获得
			this.setInsertOperInfo(bo);
			busiFileInfo.insert(bo);							
		
			fs.showPage(300, 300);
	    	
			model.addAttribute("message", "保存成功");
			model.addAttribute("FileSaver", fs);*/
	    	return "office/createWord/SaveNewFile";
		
	}
	
	/**
	 * 打开服务器PDF文件
	 * @param request
	 * @param model
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value = "openPDF", method = RequestMethod.GET)
	public String openPDF(HttpServletRequest request, Model model) throws AppException {

		/*// 按键说明：光标键、Home、End、PageUp、PageDown可用来移动或翻页；数字键盘+、-用来放大缩小；数字键盘/、*用来旋转页面。
		String path = PATH + "document" + File.separator;
		PDFCtrl pdfCtrl = new PDFCtrl(request);//定义PDFCtrl控件对象
		pdfCtrl.setServerPage(request.getContextPath()+"/poserver.zz"); //设置服务器页面
		pdfCtrl.setTheme(ThemeType.CustomStyle);//设置主题样式
*/
		//添加自定义按钮
		/*pdfCtrl.addCustomToolButton("打印", "Print()", 6);
		pdfCtrl.addCustomToolButton("-", "", 0);
		pdfCtrl.addCustomToolButton("实际大小", "SetPageReal()", 16);
		pdfCtrl.addCustomToolButton("适合页面", "SetPageFit()", 17);
		pdfCtrl.addCustomToolButton("适合宽度", "SetPageWidth()", 18);
		pdfCtrl.addCustomToolButton("-", "", 0);
		pdfCtrl.addCustomToolButton("首页", "FirstPage()", 8);
		pdfCtrl.addCustomToolButton("上一页", "PreviousPage()", 9);
		pdfCtrl.addCustomToolButton("下一页", "NextPage()", 10);
		pdfCtrl.addCustomToolButton("尾页", "LastPage()", 11);
		pdfCtrl.addCustomToolButton("-", "", 0);
		pdfCtrl.addCustomToolButton("左转", "RotateLeft()", 12);
		pdfCtrl.addCustomToolButton("右转", "RotateRight()", 13);
		pdfCtrl.addCustomToolButton("-", "", 0);
		pdfCtrl.addCustomToolButton("放大", "ZoomIn()", 14);
		pdfCtrl.addCustomToolButton("缩小", "ZoomOut()", 15);
		pdfCtrl.addCustomToolButton("-", "", 0);
		pdfCtrl.addCustomToolButton("全屏", "SwitchFullScreen()", 4);
		//设置禁止拷贝
		pdfCtrl.setAllowCopy(false);*/

		//String docPath = StringUtils.replace(path+File.separator, "/", "\\\\");
		String fileName = request.getParameter("fileName");
		String fileId = request.getParameter("fileId");
		if(StringUtils.isNotEmpty(fileId)){
			
			IFileService busiFileInfo = (IFileService)SpringContextHolder.getBean(IFileService.BEAN_ID); 
			FileVo fileVo = busiFileInfo.getById(fileId);
			if(fileVo ==null){
				throw new AppException("文件不存在，或已被删除");
			}
			fileName = fileVo.getFileServerName();
			//path = PATH + "common" + File.separator;
		}
		//--------------需要设置路径 与文件名称
		//pdfCtrl.webOpen(docPath+ fileName);//打开文件
		//pdfCtrl.setTagId("PDFCtrl1");//此行必须
		
		return "office/editWord/OpenPDF";
	}

}
